<?php

class IndexController extends MyClass_Action {
	protected $controller;
	protected $action;
	
	public function init() {
		
		$this->_user = $this->checkLogin ();
		//print_r($this->_user);exit;
		/* Initialize action controller here */
		$this->controller = $this->getRequest ()->getControllerName ();
		$this->action = $this->getRequest ()->getActionName ();
		
		$this->commonTemplates ();
		
		$this->_smarty->assign ( 'controller', $this->controller );
		$this->_smarty->assign ( 'action', $this->action );
		$this->_smarty->assign ( '_user', $this->_user );

		$this->_smarty->assign ( 'title', 'Home' );
		
		//get database
		$this->getDb ( 'osmdb' );
		
					
	//verify rights
	//	if (isset ( $this->_user->uid ))
	//		$this->_redirect ( '/' );
	

	}
	
	public function indexAction() {
		try {
			//branch name
			if ($this->_user->branch_id) {
				$sql = 'select `branch_name` from `arafmi_branch` where `branch_id`=' . $this->_user->branch_id;
				$branch_name = $this->_dbAdapter->fetchOne ( $sql );
			} else {
				$branch_name = 'admin';
			}
			//print_r($userinfo);exit;
			$this->_smarty->assign ( 'branch_name', $branch_name );
			
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Caution', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	public function normalAction() {
		try {
			$date = date('l jS \of F Y h:i:s A');
			
			$this->_smarty->assign ( 'date', $date );
			$this->_smarty->display ( $this->controller . '/' . $this->action . '.tpl' );
		} catch ( Exception $e ) {
			$this->feedback ( $e->getMessage (), 'Attention', 'javascript:window.history.back();', 'warning' );
		}
	}
	
	//reserve statement as below
	public function noRouteAction() {
		$this->_redirect ( '/' );
	}
	
	public function __call($method, $args) {
		if ('Action' == substr ( $method, - 6 )) {
			$url = '/' . $this->controller . '/index';
			return $this->_redirect ( $url );
		}
		
		throw new Exception ( 'Invalid method' );
		
		// all other methods throw an exception
		throw new Exception ( 'Invalid method "' . $method . '" called' );
	}
}

